package com.heima.handlerException;


import com.heima.exception.CustomException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

//@ControllerAdvice ：控制器增强，只要出现异常，就找它
//@ExceptionHandler：指定 要处理的异常的类型
@ControllerAdvice
public class CustomExceptionResolver {

    /**
     * 处理自定义异常类型
     * 括号中的参数就是抛出的异常对象
     */
    @ExceptionHandler(value = {CustomException.class})
    public ModelAndView customExceptionHandler(CustomException e){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("异常信息为："+e.getMessage());
        modelAndView.addObject("errMsg",e.getMessage());
        modelAndView.setViewName("error");
        return modelAndView;
    }

    @ExceptionHandler(value = {Exception.class})
    public ModelAndView exceptionHandler(Exception e){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("异常信息为："+e.getMessage());
        modelAndView.addObject("errMsg","系统异常，请联系管理员");
        modelAndView.setViewName("error");
        return modelAndView;
    }

}
